package problem;

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Test {
    static int[][] paths = new int[24][4];
    static int[] masks = new int[24];
    static int index = 0;
    static long[] max_gains = new long[26];
    static int max_depth = 0;
    static boolean[][] visited = new boolean[1024][24];

    private static void backTrace( int[] current, int cnt, boolean[] used){
        if(cnt==4) {
            for(int j=0; j<4; j++){
                paths[index][j] = current[j];
            }
            index ++;
        }
        for(int i=0; i<4; i++){
            if(used[i]){
                continue;
            } else {

                current[cnt] = i+1;
                used[i] = true;
                backTrace(current, cnt + 1, used);
                used[i] = false;
            }
        }
    }

    private static void dfs(int[] money, int mask, long cur_gain, int cur_index, int depth){
        // if(cur_gain < 0 && max_gains[depth] >= 0) return;
        if(depth >= max_depth) return;
        int chosen_mask = masks[cur_index];
        int new_mask = chosen_mask | mask;
        if(!visited[new_mask][depth+1]){
            visited[new_mask][depth+1] = true;
            int op = 1;
            long new_gain = 0;
            for(int i=0; i<9; i++){
                if((op & new_mask) > 0){
                    new_gain += money[i];
                }
                op = op << 1;
            }
            if(new_gain >= cur_gain){

                max_gains[depth+1] = Math.max(max_gains[depth+1] ,new_gain);

                for(int j=cur_index + 1; j<24; j++){
                    dfs(money, new_mask, new_gain, j, depth+1);
                }

            }
        }
        return;

    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = 10;
        int k = 5;
        int stage_num = n / 10;
//        int[][] arr = new int[stage_num][10];
//        arr[stage_num-1][9] = in.nextInt();
//        for(int i=0; i<stage_num-1; i++){
//            for(int j=0; j<10; j++)
//                arr[i][j] = in.nextInt();
//        }
//        for(int j=0; j<9; j++){
//            arr[stage_num-1][j] = in.nextInt();
//        }
//        for(int i=0; i<stage_num; i++){
//            for(int j=0; j<10; j++){
//                if(j%3==0)
//                    arr[i][j] = 1;
//                else if(j%3==1)
//                    arr[i][j] = -2;
//                else
//                    arr[i][j] = 3;
//            }
//        }
        int [][] arr = {{-1, -1, 2, 3, 4, -9, -9, -1, 3, -1}};
        int[] current = new int[4];
        boolean[] used = new boolean[4];
        backTrace(current, 0, used);
        for(int i=0; i<24; i++){
            int cur_index = 0;
            int mask = 0;
            for(int j=0; j<3; j++){
                cur_index += paths[i][j];
                mask |= 1 << (cur_index-1);
            }
            masks[i] = mask;
        }

        long res = 0;

        int mid = 0;
        int iter_num = 0;
        if(k % stage_num == 0){
            max_depth = k / stage_num;
            mid = stage_num;
        } else {
            max_depth = k / stage_num + 1;
            mid = k % stage_num;
            // if(max_depth >= 24) mid = stage_num;
        }
        max_depth = Math.min(25, max_depth);
        iter_num = max_depth;

        long min_inf = -0x7fffffffffffffffL-1;
        for(int i=0; i<stage_num; i++){
            if(i==mid){
                max_depth -= 1;}
            if(max_depth <= 0) continue;
            for(int j=0; j<1024; j++){
                for(int m=0; m<24; m++){
                    visited[j][m] = false;
                }
            }
            for(int j=0; j<=max_depth ; j++){
                max_gains[j] = min_inf;
            }
            for(int j=0; j<24; j++){
                dfs(arr[i], 0, min_inf, j, 0);
            }
            long stage_max_gain = min_inf;
            for(int j=1; j<=max_depth; j++){
                stage_max_gain = Math.max(stage_max_gain, max_gains[j]);
            }
            res += stage_max_gain + arr[i][9];

        }

        System.out.print(res);
    }
}